package edu.hawaii.duedates.webapp;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.behavior.HeaderContributor;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Check;
import org.apache.wicket.markup.html.form.CheckGroup;
import org.apache.wicket.markup.html.form.CheckGroupSelector;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.util.time.Duration;
import edu.hawaii.duedates.datastore.LibraryChoice;
import edu.hawaii.duedates.tools.Alerter;

/**
 * Alerts page.
 * 
 * @author Daniel Arakaki, Jeho Jung, Arthur Shum, Daniel Tian.
 */
@SuppressWarnings("unchecked")
public class AlertsPage extends SecurePage {
    
  /** Support serialization. */
  private static final long serialVersionUID = 1L;
  
  /**
   * Creates the alerts page.
   */
  public AlertsPage() {
        
    // populates ArrayList of LibraryChoice objects with the user's list of libraries
    List<LibraryChoice> libChoices = LibraryChoice.createList(getUser().getLibraryList());
    
    // create the checkbox group
    final CheckGroup group = new CheckGroup("group", new ArrayList<LibraryChoice>());
    // add checkbox to select/deselect all
    group.add(new CheckGroupSelector("groupselector"));
    // create a table from the ArrayList of LibraryChoice objects
    group.add(new ListView("libraries", libChoices) {
      private static final long serialVersionUID = 1L;
      /** For each LibraryChoice, add a checkbox and a textfield to enter filter info. */
      @Override
      protected void populateItem(ListItem item) {
        LibraryChoice choice = (LibraryChoice) item.getModelObject();
        item.add(new Check("checkbox", new Model(choice)));
        item.add(new Label("libraryname", choice.getLibrary().getName() ));
        item.add(new TextField("filter", new PropertyModel(choice, "filter") ));
      }
    });
    
    // create a form for the checkboxes and textfields
    Form form = new Form("form");
    
    form.add(group); // add the checkbox group to the form
      
    form.add(new Button("enable") {
      private static final long serialVersionUID = 1L;
      @Override
      public void onSubmit() {
        List<LibraryChoice> choices = (List<LibraryChoice>) group.getModelObject();
        if (choices.isEmpty()) {
          info("No libraries selected.");
        }
        else {
          getCurrentSession().getNewTask().run(Duration.ONE_DAY, 
              new Alerter(getUser().getEmail(), getCurrentSession().getSmtpServer(), choices));
          getCurrentSession().setAlertsEnabled(true);
          info("Alerts enabled.");
        }
      }
      public boolean isVisible() {
        return !getCurrentSession().alertsEnabled();
      }      
    });

    form.add(new Button("disable") {
      private static final long serialVersionUID = 1L;
      @Override
      public void onSubmit() {
        getCurrentSession().getTask().interrupt();
        getCurrentSession().setAlertsEnabled(false);
        info("Alerts disabled");
      }
      public boolean isVisible() {
        return getCurrentSession().alertsEnabled();
      }
    });

    add(HeaderContributor.forCss(
        edu.hawaii.duedates.webapp.AlertsPage.class, "GlobalStyle.css", "screen"));    
    add(HeaderContributor.forCss(
        edu.hawaii.duedates.webapp.AlertsPage.class, "AlertsPage.css", "screen"));
    
    add(new Link("home") {
      private static final long serialVersionUID = 1L;
      @Override
      public void onClick() {
        setResponsePage(HomePage.class);
      }
    });

    add(new Link("logout") {
      private static final long serialVersionUID = 1L;
      @Override
      public void onClick() {
        getCurrentSession().invalidate();
        setResponsePage(LoginPage.class);
      }      
    });
    
    add(form); // add the form to the page
    add(new FeedbackPanel("feedback"));
    
    if (getCurrentSession().alertsEnabled()) {
      info("Alerts are currently enabled.");
    }
    
  }
  
}
